<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目编辑 - 见闻间后台管理</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.15.13/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/element-ui@2.15.13/lib/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios@0.21.1/dist/axios.min.js"></script>
    <style>
        .app-container { padding: 20px; }
    </style>
</head>
<body>
    <div id="app" class="app-container">
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span v-text="pageTitle"></span>
                <el-button 
                    type="primary" 
                    size="small" 
                    style="float: right; margin-top: -5px;"
                    @click="save"
                >
                    保存
                </el-button>

<!--                :disabled="viewOnly"-->
            </div>

            <el-form 
                ref="form" 
                :model="formData" 
                label-width="100px"
            >
                <el-form-item label="分类ID" v-if="isEdit">
                    <el-input v-model="formData.id" disabled></el-input>
                </el-form-item>

                <el-form-item label="名称">
<!--                    :disabled="viewOnly"-->
                    <el-input 
                        v-model="formData.name" 
                        placeholder="请输入项目名称"

                        maxlength="50"
                        show-word-limit
                    ></el-input>

                </el-form-item>

                <el-form-item label="APPID">
                    <!--                    :disabled="viewOnly"-->
                    <el-input
                            v-model="formData.app_id"
                            placeholder="请输入APPID名称"

                            maxlength="50"
                            show-word-limit
                    ></el-input>
                </el-form-item>
                    <el-form-item label="密钥">
                        <!--                    :disabled="viewOnly"-->
                        <el-input
                                v-model="formData.secret"
                                placeholder="请输入密钥"

                                maxlength="50"
                                show-word-limit
                        ></el-input>
                </el-form-item>
            </el-form>
        </el-card>
    </div>

    <script>
        // 调试信息
        console.log('Vue版本:', Vue.version);
        console.log('Element UI已加载:', typeof ELEMENT !== 'undefined');
        console.log('Axios已加载:', typeof axios !== 'undefined');
        
        // 获取URL参数的简单方法
        function getUrlParam(name) {
            const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
            const r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURIComponent(r[2]);
            return null;
        }

        // 等待DOM加载完成
        document.addEventListener('DOMContentLoaded', function() {
            console.log('DOM已加载完成，开始初始化Vue');
            
            // 简单的Vue实例
            var app = new Vue({
                el: '#app',
                data: {
                    formData: {
                        id: '',
                        name: '',
                        app_id: '',
                        secret: ''
                    },
                    isEdit: false,
                    viewOnly: false,
                    pageTitle: '新增领域分类'
                },
                mounted() {
                    console.log('Vue实例已挂载');
                    this.initData();
                },
                methods: {
                    initData() {
                        const id = getUrlParam('id');
                        const view = getUrlParam('view');
                        
                        this.isEdit = !!id;
                        this.viewOnly = !!view;
                        this.pageTitle = this.isEdit ? '编辑领域分类' : '新增领域分类';
                        
                        console.log('初始化数据:', { id, view, isEdit: this.isEdit, viewOnly: this.viewOnly });
                        
                        if (this.isEdit) {
                            this.formData.id = id;
                            this.fetchCateDetail(id);
                        }
                    },
                    fetchCateDetail(id) {
                        console.log('获取分类详情，ID:', id);
                        axios.get('/science/project/detail', { params: { id: id } })
                            .then(response => {
                                console.log('获取详情响应:', response.data);
                                if (response.data && response.data.code === 200 && response.data.data) {
                                    this.formData = response.data.data;
                                } else {
                                    alert('获取数据失败：' + (response.data ? response.data.msg || '未知错误' : '无响应数据'));
                                }
                            })
                            .catch(error => {
                                console.error('获取数据错误:', error);
                                alert('网络错误，请检查连接');
                            });
                    },
                    save() {
                        console.log('保存数据:', this.formData);
                        
                        if (!this.formData.name.trim()) {
                            alert('请输入分类名称');
                            return;
                        }
                        
                        const requestData = { name: this.formData.name, app_id: this.formData.app_id, secret: this.formData.secret };
                        if (!this.formData.app_id.trim()) {
                            alert('请输入APPID名称');
                            return;
                        }
                        if (!this.formData.secret.trim()) {
                            alert('请输入密钥');
                            return;
                        }

                        const url = this.isEdit ? '/science/project/update' : '/science/project/add';
                        const method = this.isEdit ? 'post' : 'post';
                        const params = this.isEdit ? { id: this.formData.id } : {};
                        
                        console.log('发送请求:', { url, method, params, data: requestData });
                        
                        axios[method](url, requestData, { params: params })
                            .then(response => {
                                console.log('保存响应:', response.data);
                                if (response.data && response.data.code === 200) {
                                    alert(this.isEdit ? '更新成功' : '创建成功');
                                    setTimeout(() => window.history.back(), 1000);
                                } else {
                                    alert('保存失败：' + (response.data ? response.data.msg || '未知错误' : '无响应数据'));
                                }
                            })
                            .catch(error => {
                                console.error('保存错误:', error);
                                alert('网络错误，请检查连接');
                            });
                    }
                }
            });
            
            console.log('Vue实例已创建:', app);
        });
    </script>
</body>
</html>